// @ts-ignore
import ajax from '/public/ts/ajax.ts';

interface userDataUser {
  'msg': string,
  'id': number,
  'username': string,
  'nickname': string,
  'createTime': number,
  'identity': string,
  'gender': '',
  'age': ''

}

interface userData {
  'message': string,
  'code': number,
  'user': userDataUser,
  'tips': string,
  'token': string
}

function login(): void {
  console.log(1);
  const username: string | null = (document.getElementById('username') as HTMLInputElement).value;
  const password: string | null = (document.getElementById('password') as HTMLInputElement).value;
  console.log(username);
  ajax({
    url: '/users/login',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: {
      username,
      password,
    },
  }).then((r: { data: userData; }) => {
    if (r.data.code === 1) {
      localStorage.setItem('token', r.data.token);
      location.href = '../page/list.html';
    }
  }).catch((err: any) => {
    console.log(err);
  });
}

const BLogin: HTMLElement | null = document.getElementById('BLogin');
if (BLogin)
  BLogin.onclick = login;